package cn.stylefeng.guns.modular.xlb.service.impl;

import cn.hutool.json.JSONUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.core.util.DateUtil2;
import cn.stylefeng.guns.modular.basic.entity.*;
import cn.stylefeng.guns.modular.basic.model.params.*;
import cn.stylefeng.guns.modular.basic.model.result.*;
import cn.stylefeng.guns.modular.basic.service.*;
import cn.stylefeng.guns.modular.order.entity.StorageTaskXlb;
import cn.stylefeng.guns.modular.order.model.params.OrderTransportDetailedParam;
import cn.stylefeng.guns.modular.order.model.params.OrderTransportParam;
import cn.stylefeng.guns.modular.order.service.OrderTransportDetailedService;
import cn.stylefeng.guns.modular.order.service.OrderTransportService;
import cn.stylefeng.guns.modular.rest.xlbapi.vo.ReceStoreVo;
import cn.stylefeng.guns.modular.rest.xlbapi.vo.SyncDepartOrderIby;
import cn.stylefeng.guns.modular.transport.model.params.TDispatchParam;
import cn.stylefeng.guns.modular.transport.model.params.TransportDispatchParam;
import cn.stylefeng.guns.modular.transport.model.params.TransportReceiptParam;
import cn.stylefeng.guns.modular.transport.service.TDispatchService;
import cn.stylefeng.guns.modular.transport.service.TransportDispatchService;
import cn.stylefeng.guns.modular.transport.service.TransportReceiptService;
import cn.stylefeng.guns.modular.xlb.entity.XlbApiDepartOrder;
import cn.stylefeng.guns.modular.xlb.mapper.XlbApiDepartOrderMapper;
import cn.stylefeng.guns.modular.xlb.model.params.XlbApiDepartOrderParam;
import cn.stylefeng.guns.modular.xlb.model.result.XlbApiDepartOrderResult;
import  cn.stylefeng.guns.modular.xlb.service.XlbApiDepartOrderService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 同步发车单信息 服务实现类
 * </p>
 *
 * @author zhangzh
 * @since 2025-06-17
 */
@Service
public class XlbApiDepartOrderServiceImpl extends ServiceImpl<XlbApiDepartOrderMapper, XlbApiDepartOrder> implements XlbApiDepartOrderService {

    @Autowired
    private BasicCarrierService basicCarrierService;

    @Autowired
    private BasicLineService basicLineService;

    @Autowired
    private BasicPrivatecarService basicPrivatecarService;

    @Autowired
    private BasicPutgoodsService basicPutgoodsService;

    @Autowired
    private BasicAddressService basicAddressService;

    @Autowired
    private BasicDriverService basicDriverService;

    @Autowired
    private BasicOperateService basicOperateService;

    @Autowired
    private BasicGoodsService basicGoodsService;

    @Autowired
    private OrderTransportService orderTransportService;

    @Autowired
    private OrderTransportDetailedService orderTransportDetailedService;

    @Autowired
    private TransportDispatchService transportDispatchService;

    @Autowired
    private TransportReceiptService transportReceiptService;

    @Autowired
    private TDispatchService tDispatchService;

    @Override
    public void add(XlbApiDepartOrderParam param){
        XlbApiDepartOrder entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(XlbApiDepartOrderParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(XlbApiDepartOrderParam param){
        XlbApiDepartOrder oldEntity = getOldEntity(param);
        XlbApiDepartOrder newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public XlbApiDepartOrderResult findBySpec(XlbApiDepartOrderParam param){
        return null;
    }

    @Override
    public List<XlbApiDepartOrderResult> findListBySpec(XlbApiDepartOrderParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(XlbApiDepartOrderParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class,timeout = 1800)
    public ResponseData syncDepartOrder(SyncDepartOrderIby syncDepartOrderIby, BasicCustomer basicCustomer) {
        // 判断承运商是否存在，不存在则新增
        Integer carrierId = -1 ;
        if(ToolUtil.isNotEmpty(syncDepartOrderIby.getCarrierName())){
            BasicCarrier carrierObj = basicCarrierService.lambdaQuery()
                    .eq(BasicCarrier::getCarrierName, syncDepartOrderIby.getCarrierName())
                    .eq(BasicCarrier::getOperateId, basicCustomer.getCustomerOperateId())
                    .last("LIMIT 1")
                    .one();

            if(ToolUtil.isEmpty(carrierObj)){
                BasicCarrierParam addCarrierObj = new BasicCarrierParam();
                addCarrierObj.setOperateId(basicCustomer.getCustomerOperateId().longValue());
                addCarrierObj.setOperateName(basicCustomer.getCustomerOperateName());
                addCarrierObj.setDeliverId(1L);
                addCarrierObj.setDeptId(basicCustomer.getDeptId());
                addCarrierObj.setCarrierName(syncDepartOrderIby.getCarrierName());
                addCarrierObj.setCreateUser("新零帮接口");
                addCarrierObj.setCreateTime(new Date());
                addCarrierObj.setV1("1");
                basicCarrierService.add(addCarrierObj);
                carrierObj = basicCarrierService.lambdaQuery()
                        .eq(BasicCarrier::getCarrierName, syncDepartOrderIby.getCarrierName())
                        .eq(BasicCarrier::getOperateId, basicCustomer.getCustomerOperateId())
                        .last("LIMIT 1")
                        .one();

            }
            carrierId = carrierObj.getId();

        }

        // 判断车辆是否存在，不存在则新增
        Integer carId = -1;
        if(ToolUtil.isNotEmpty(syncDepartOrderIby.getCarNumber())){
            BasicPrivatecar basicPrivatecar = basicPrivatecarService.lambdaQuery()
                    .eq(BasicPrivatecar::getPrivatecarCarnumber, syncDepartOrderIby.getCarNumber())
                    .eq(BasicPrivatecar::getOperateId, basicCustomer.getCustomerOperateId())
                    .last("LIMIT 1")
                    .one();

            if(ToolUtil.isEmpty(basicPrivatecar)){
                BasicPrivatecarParam addPrivatecarObj = new BasicPrivatecarParam();
                addPrivatecarObj.setOperateId(basicCustomer.getCustomerOperateId().longValue());
                addPrivatecarObj.setOperateName(basicCustomer.getCustomerOperateName());
                addPrivatecarObj.setDeliverId(1L);
                addPrivatecarObj.setDeptId(basicCustomer.getDeptId());
                addPrivatecarObj.setPrivatecarCarnumber(syncDepartOrderIby.getCarNumber());
                addPrivatecarObj.setCreateUser(-1L);
                addPrivatecarObj.setCreateTime(new Date());
                addPrivatecarObj.setPrivatecarState("空闲");
                this.basicPrivatecarService.add(addPrivatecarObj);
                basicPrivatecar = basicPrivatecarService.lambdaQuery()
                        .eq(BasicPrivatecar::getPrivatecarCarnumber, syncDepartOrderIby.getCarNumber())
                        .eq(BasicPrivatecar::getOperateId, basicCustomer.getCustomerOperateId())
                        .last("LIMIT 1")
                        .one();
            }

            carId = basicPrivatecar.getId();
        }
        // 判断司机是否存在，不存在则新增
        Integer driverId = -1;
        if(ToolUtil.isNotEmpty(syncDepartOrderIby.getDriverName())){
            BasicDriver basicDriver = basicDriverService.lambdaQuery()
                    .eq(BasicDriver::getDriverName, syncDepartOrderIby.getDriverName())
                    .eq(BasicDriver::getDriverOperateId, basicCustomer.getCustomerOperateId())
                    .last("LIMIT 1")
                    .one();
            if(ToolUtil.isEmpty(basicDriver)){
                BasicDriverParam addDriverObj = new BasicDriverParam();
                addDriverObj.setDriverOperateId(basicCustomer.getCustomerOperateId());
                addDriverObj.setDriverOperateName(basicCustomer.getCustomerOperateName());
                addDriverObj.setDeliverId(1L);
                addDriverObj.setDeptId(basicCustomer.getDeptId());
                addDriverObj.setDriverName(syncDepartOrderIby.getDriverName());
                addDriverObj.setCreateUser(-1L);
                addDriverObj.setCreateTime(new Date());
                addDriverObj.setTel(syncDepartOrderIby.getDriverPhoneNum());
                basicDriverService.add(addDriverObj);
                basicDriver = basicDriverService.lambdaQuery()
                        .eq(BasicDriver::getDriverName, syncDepartOrderIby.getDriverName())
                        .eq(BasicDriver::getDriverOperateId, basicCustomer.getCustomerOperateId())
                        .last("LIMIT 1")
                        .one();
            }
            driverId = basicDriver.getId();
        }

        //项目信息
        BasicOperate basicOperate = basicOperateService.getById(basicCustomer.getCustomerOperateId());//分公司信息
        // 装车单位
        BasicPutgoodsParam basicPutgoodsParam = new BasicPutgoodsParam();
        basicPutgoodsParam.setUnitCategory("发货");
        basicPutgoodsParam.setDeptId(basicCustomer.getDeptId());
        BasicPutgoodsResult sh = basicPutgoodsService.findBySpec(basicPutgoodsParam, Long.parseLong(basicCustomer.getCustomerOperateId().toString()));//发货
        if (sh == null) {
            return ResponseData.error("发货单位不存在，请联系先飞维护！");
        }

        BasicAddressParam basicAddressParam = new BasicAddressParam();
        basicAddressParam.setPutgoodsId(Long.parseLong(sh.getId().toString()));
        BasicAddressResult addressResult = basicAddressService.findBySpec(basicAddressParam);
        if (addressResult == null) {
            return ResponseData.error("发货单位地址不存在，请联系先飞维护！");
        }
        BasicGoodsParam goodsParam = new BasicGoodsParam();
        goodsParam.setGoodsCustomerId(basicCustomer.getId());
        goodsParam.setGoodsNumber("sp1");
        BasicGoodsResult sp1 = basicGoodsService.findBySpec(goodsParam, null);//商品
        if (sp1 == null) {
            return ResponseData.error("sp1商品不存在，请联系先飞维护！");
        }

        String lineNames = "";
        List<String> orderTransportList = new ArrayList<>();
        StringBuffer storeNames = new StringBuffer();
        if(ToolUtil.isNotEmpty(syncDepartOrderIby.getReceStores())){
            int n = 0;
            for(ReceStoreVo obj : syncDepartOrderIby.getReceStores()){
                if(storeNames.length() < 1){
                    storeNames.append(obj.getReceStoreName());
                }else{
                    storeNames.append(",").append(obj.getReceStoreName());
                }

                n ++ ;
                String customerOrder = syncDepartOrderIby.getDepartOrderId() + "-" + n;
                orderTransportList.add(customerOrder);
                OrderTransportParam addp = new OrderTransportParam();
                addp.setOperateId(Long.parseLong(basicCustomer.getCustomerOperateId().toString()));//分公司id
                addp.setOperateName(basicOperate.getOperateName());//公司名称
                addp.setCustomerOrder(customerOrder);
                addp.setTransportNumber(customerOrder);
                addp.setOrderNumber(customerOrder);
                addp.setXlbClientId(obj.getReceStoreId());
                addp.setOrderSource("新零帮");
                addp.setCustomerId(Long.parseLong(basicCustomer.getId().toString()));//客户id
                addp.setCustomerName(basicCustomer.getCustomerName());//客户名称
                addp.setUndertime(DateUtil2.getDateS());//下单时间
                addp.setSetMentiontime(cn.stylefeng.guns.core.util.DateUtil.getDate());//发货日期
                addp.setStockPort("新零帮发车");
                addp.setSetUnit(sh.getPutgoodsName());//装货单位
                addp.setSetUnitId(Long.parseLong(sh.getId().toString()));//装货单位id
                addp.setSetLocation(addressResult.getAddress());//装货地址
                addp.setSetLocationId(Long.parseLong(addressResult.getId().toString()));//装货地址id
                addp.setSetPeople(addressResult.getContacts());//联系人
                addp.setSetTel(addressResult.getTel());//联系电话
                //处理单位和地址
                StorageTaskXlb xlb = new StorageTaskXlb();
                xlb.setClientName(obj.getReceStoreName());
                xlb.setClientId(obj.getReceStoreId());
                this.toDealWith4(addp,basicCustomer,xlb);

                addp.setDeptId(basicCustomer.getDeptId());
                addp.setDeliverId(1L);
                addp.setCreateName("新零帮");
                addp.setCreateTime(new Date());
                addp.setSubmitName("新零帮");
                addp.setTooNum(obj.getGoodsBoxNum());
                addp.setTooVolume(obj.getGoodsVol().toString());
                addp.setTotalNum(obj.getGoodsBoxNum());
                addp.setGoodsNum(obj.getGoodsBoxNum());
                addp.setTotalVolume(obj.getGoodsVol().toString());
                addp.setTstate("已发运");
                //新增货运明细
                OrderTransportDetailedParam outTranDetailed = new OrderTransportDetailedParam();
                outTranDetailed.setGoodsVolume("1");
                outTranDetailed.setTransportPutvolume(obj.getGoodsVol().toString());
                outTranDetailed.setGoodsWeight("1");
                outTranDetailed.setTransportWeight(obj.getGoodsBoxNum());
                outTranDetailed.setUnitprice("1");
                outTranDetailed.setTransportMoney(obj.getGoodsBoxNum());
                outTranDetailed.setTransportNumber(addp.getTransportNumber());
                // sp1
                outTranDetailed.setGoodsName(sp1.getGoodsName());
                outTranDetailed.setGoodId(Long.parseLong(sp1.getId().toString()));
                outTranDetailed.setGoodsNumber(sp1.getGoodsNumber());
                outTranDetailed.setGoodsNo(sp1.getGoodsNo());
                outTranDetailed.setRebubbles(sp1.getGoodsHeavybubble());
                outTranDetailed.setDpacking(sp1.getGoodsPacking());
                outTranDetailed.setLargeclass(sp1.getGoodsBigtype());
                outTranDetailed.setGoodstype(sp1.getGoodsType());
                outTranDetailed.setConditions(sp1.getGoodsCondition());
                outTranDetailed.setGoodsUnit(sp1.getGoodsUnit());
                outTranDetailed.setGoodsSpecs(sp1.getGoodsSpecs());
                outTranDetailed.setBalenum(obj.getGoodsBoxNum());
                outTranDetailed.setNum(obj.getGoodsBoxNum());
                outTranDetailed.setNotnum("0");
                outTranDetailed.setNotvolume("0");
                orderTransportService.add(addp);
                orderTransportDetailedService.add(outTranDetailed);

                //新增调度明细
                TransportDispatchParam tpdp = new TransportDispatchParam();
                tpdp.setDispatchNumber(syncDepartOrderIby.getDepartOrderId());
                tpdp.setTransportNumber(customerOrder);
                tpdp.setCustomerOrder(customerOrder);
                tpdp.setCreateUserName("新零帮");
                tpdp.setGoodsId(sp1.getId().longValue());
                if(carId != null){
                    tpdp.setCarId(carId.longValue());
                }
                tpdp.setCarnumbger(syncDepartOrderIby.getCarNumber());
                tpdp.setCartel(syncDepartOrderIby.getDriverPhoneNum());
                tpdp.setDriver1(syncDepartOrderIby.getDriverName());
                tpdp.setTransportNumber(customerOrder);//货运订单号
                tpdp.setTooDeployVolume(obj.getGoodsVol().toString());//分配的体积
                tpdp.setTooDeployNum(obj.getGoodsBoxNum());//分配的数量
                tpdp.setTransportMoney("0");//金额
                tpdp.setGoodsId(sp1.getId().longValue());//商品id
                tpdp.setGoodsName(sp1.getGoodsName());//商品名称
                tpdp.setGoodsNumber(sp1.getGoodsNumber());
                tpdp.setGoodsNo(sp1.getGoodsNo());//货号
                tpdp.setGoodsVolume(sp1.getGoodsVolume());//体积
                tpdp.setGoodsPacking(sp1.getGoodsPacking());//包装
                tpdp.setGoodsSpecs(sp1.getGoodsSpecs());//规格
                if(ToolUtil.isNotEmpty(addp.getLineId())){
                    tpdp.setLineId(addp.getLineId());
                    if(ToolUtil.isNotEmpty(addp.getLineName())){
                        if(lineNames.length() > 0){
                            lineNames = lineNames + "," + addp.getLineName();
                        }else{
                            lineNames = addp.getLineName();
                        }
                    }
                }
                tpdp.setDeliverId(1L);
                tpdp.setDeptId(basicCustomer.getDeptId());
                this.transportDispatchService.add(tpdp);
                // 添加回单信息
                TransportReceiptParam transportReceiptAdd = new TransportReceiptParam();
                transportReceiptAdd.setDispatchNumber(syncDepartOrderIby.getDepartOrderId());
                transportReceiptAdd.setCarnumber(syncDepartOrderIby.getCarNumber());
                if(carId != null){
                    transportReceiptAdd.setCarId(carId.longValue());
                }
                transportReceiptAdd.setSetUnit(addp.getSetUnit());
                transportReceiptAdd.setSetLocation(addp.getSetLocation());
                transportReceiptAdd.setGetPeople(addp.getGetPeople());
                transportReceiptAdd.setGetTel(addp.getGetTel());
                transportReceiptAdd.setGetUnitid(addp.getGetUnitId());
                transportReceiptAdd.setGetUnit(addp.getGetUnit());
                transportReceiptAdd.setGetLocation(addp.getGetLocation());
                transportReceiptAdd.setGetLocationid(addp.getGetLocationId());
                transportReceiptAdd.setSdtstate("未送达");
                transportReceiptAdd.setTstate("未回传");
                transportReceiptAdd.setNum(addp.getTotalNum());
                transportReceiptAdd.setVolume(addp.getTooVolume());
                transportReceiptAdd.setDeptId(basicCustomer.getDeptId());
                transportReceiptAdd.setDeliverId(1L);
                transportReceiptAdd.setSetUnitId(addp.getSetUnitId());
                transportReceiptAdd.setSetLocationId(addp.getSetLocationId());
                transportReceiptAdd.setXlbClientId(obj.getReceStoreId());
                transportReceiptService.add(transportReceiptAdd);
            }
        }

        // 新增调度单
        //生成运输单号
        TDispatchParam dispatchParam = new TDispatchParam();
        dispatchParam.setLoadingType("仓库");
        dispatchParam.setLoadingPersonnel("");
        dispatchParam.setDianduyuan("新零帮");
        dispatchParam.setChengyunfangshi("整车");
        dispatchParam.setChengyunleixing(syncDepartOrderIby.getCarTyp());
        dispatchParam.setMileageTraveledYy(syncDepartOrderIby.getMileage().toString());
        dispatchParam.setRemain("0");
        dispatchParam.setDispatchNumber(syncDepartOrderIby.getDepartOrderId());
        dispatchParam.setMileageTraveled(syncDepartOrderIby.getMileage().toString());
        dispatchParam.setOperateId(basicOperate.getId().longValue());
        dispatchParam.setOperateName(basicOperate.getOperateName());
        dispatchParam.setLineName(lineNames);
        dispatchParam.setFayunTimes(syncDepartOrderIby.getDepartTm());
        dispatchParam.setStartDate(DateUtil.getDate());
        if(carrierId != null){
            dispatchParam.setCarrierId(carrierId.longValue());
            dispatchParam.setCarrierName(syncDepartOrderIby.getCarrierName());
        }
        if(carId != null){
            dispatchParam.setCarId(carId.longValue());
        }
        dispatchParam.setChexing(syncDepartOrderIby.getCarTyp());
        dispatchParam.setSiji(syncDepartOrderIby.getDriverName());
        dispatchParam.setSijidianhua(syncDepartOrderIby.getDriverPhoneNum());
        dispatchParam.setCarnumber(syncDepartOrderIby.getCarNumber());
        dispatchParam.setTooNum(syncDepartOrderIby.getGoodsBoxNumSum());
        dispatchParam.setToovolume(syncDepartOrderIby.getGoodsVolSum().toString());
        dispatchParam.setTstate("待定价");
        dispatchParam.setReturnTstate("待回单");
        dispatchParam.setCalculateShippingCost("否");
        dispatchParam.setConsultMoney("0");
        dispatchParam.setFreightMoney("0");
        dispatchParam.setDeptId(basicCustomer.getDeptId());
        dispatchParam.setGetUnits(storeNames.toString());
        dispatchParam.setDeliverId(1L);
        tDispatchService.add(dispatchParam);
        // 添加记录
        XlbApiDepartOrderParam addObj = new XlbApiDepartOrderParam();
        addObj.setDepartOrderId(syncDepartOrderIby.getDepartOrderId());
        addObj.setApiTm(new Date());
        addObj.setDataJson(JSONUtil.toJsonStr(syncDepartOrderIby));
        this.add(addObj);
        return ResponseData.success();
    }

    private void toDealWith4(OrderTransportParam addp, BasicCustomer r, StorageTaskXlb xlb) {
        //查询卸货单位 是否存在
        BasicPutgoods bpgp3 = new BasicPutgoods();
        bpgp3.setPutgoodsCustomerId(r.getId());//客户id
        bpgp3.setPutgoodsName(xlb.getClientName());//单位名称
        BasicPutgoods bputr2 = basicPutgoodsService.lambdaQuery()
                .eq(BasicPutgoods::getPutgoodsCustomerId, r.getId())
                .eq(BasicPutgoods::getPutgoodsName, xlb.getClientName())
                .last("LIMIT 1")
                .one();
        if (bputr2 == null) {
            bpgp3.setPutgoodsOperateName(r.getCustomerName());//客户名称
            bpgp3.setDeliverId(r.getDeliverId());
            bpgp3.setDeptId(r.getDeptId());
            basicPutgoodsService.save(bpgp3);
            addp.setGetUnit(xlb.getClientName());//卸货单位
            addp.setGetUnitId(Long.parseLong(bpgp3.getId().toString()));//卸货单位id

            BasicAddress bap3 = new BasicAddress();
            bap3.setPutgoodsName(xlb.getClientName());
            bap3.setPutgoodsId(Long.parseLong(bpgp3.getId().toString()));
            bap3.setSheng(xlb.getClientProvinceName());
            bap3.setShi(xlb.getClientCityName());
            bap3.setQu(xlb.getClientDistrictName());
            if (ToolUtil.isNotEmpty(xlb.getClientAddress())) {
                bap3.setAddress(xlb.getClientAddress());
            } else {
                bap3.setAddress(xlb.getClientName());
            }
            bap3.setLongitude("0");
            bap3.setLatitude("0");
            bap3.setSwitchWl("1");
            bap3.setSwitchBj("1500");//提货围栏半径 默认100米
            bap3.setSwitchBj2("2000");//收货围栏半径 默认100米
            basicAddressService.save(bap3);
            addp.setGetLocationId(Long.parseLong(bap3.getId().toString()));//卸货地址id
            addp.setGetLocation(bap3.getAddress());//卸货地址
        } else {
            addp.setGetUnit(xlb.getClientName());//卸货单位
            addp.setGetUnitId(Long.parseLong(bputr2.getId().toString()));//卸货单位id
            //查询卸货地点 是否存在
            BasicAddress bap3 = new BasicAddress();
            bap3.setPutgoodsId(Long.parseLong(bputr2.getId().toString()));//单位id
            BasicAddress bar2 = basicAddressService.lambdaQuery()
                    .eq(BasicAddress::getPutgoodsId, bputr2.getId())
                    .last("LIMIT 1")
                    .one();

            if (bar2 != null) {
                addp.setGetLocationId(Long.parseLong(bar2.getId().toString()));//卸货地址id
                addp.setGetLocation(bar2.getAddress());//卸货地址
                addp.setGetPeople(bar2.getContacts());//联系人
                addp.setGetTel(bar2.getTel());//联系电话
                addp.setKilometer(bar2.getKilometer());
                addp.setLineId(bar2.getLineId());
                addp.setLineName(bar2.getLineName());
            } else {//如果没有地址 ，  卸货地址为 单位名称
                bap3.setSheng(xlb.getClientProvinceName());
                bap3.setShi(xlb.getClientCityName());
                bap3.setQu(xlb.getClientDistrictName());
                if (ToolUtil.isNotEmpty(xlb.getClientAddress())) {
                    bap3.setAddress(xlb.getClientAddress());
                } else {
                    bap3.setAddress(bputr2.getPutgoodsName());
                }
                bap3.setPutgoodsName(bputr2.getPutgoodsName());//单位名称
                bap3.setLongitude("0");
                bap3.setLatitude("0");
                bap3.setSwitchWl("1");
                bap3.setSwitchBj("1500");//提货围栏半径 默认100米
                bap3.setSwitchBj2("2000");//收货围栏半径 默认100米
                basicAddressService.save(bap3);
                addp.setGetLocation(bap3.getAddress());//卸货地址
                addp.setGetLocationId(Long.parseLong(bap3.getId().toString()));//卸货地址id
            }
        }
    }

    private Serializable getKey(XlbApiDepartOrderParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private XlbApiDepartOrder getOldEntity(XlbApiDepartOrderParam param) {
        return this.getById(getKey(param));
    }

    private XlbApiDepartOrder getEntity(XlbApiDepartOrderParam param) {
        XlbApiDepartOrder entity = new XlbApiDepartOrder();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
